package com.hospital.controller;

import com.google.gson.Gson;
import com.hospital.dao.DepartmentDao;
import com.hospital.dao.DoctorViewDao;
import com.hospital.dao.impl.DepartmentDaoImpl;
import com.hospital.dao.impl.DoctorViewDaoImpl;
import com.hospital.model.Department;
import com.hospital.model.DoctorView;
import com.hospital.util.LoggerUtil;
import org.slf4j.Logger;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 管理员管理医生信息的Servlet
 * 处理管理员查看和管理医生信息的请求
 */
@WebServlet("/admin/*")
public class AdminDoctorServlet extends HttpServlet {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerUtil.getLogger();
    
    /**
     * 医生视图数据访问对象，用于处理医生信息的数据库操作
     */
    private DoctorViewDao doctorViewDao = new DoctorViewDaoImpl();
    
    /**
     * 科室数据访问对象，用于处理科室信息的数据库操作
     */
    private DepartmentDao departmentDao = new DepartmentDaoImpl();
    
    /**
     * JSON转换工具对象，用于处理JSON数据的序列化和反序列化
     */
    private Gson gson = new Gson();

    /**
     * 处理获取医生信息的GET请求
     * @param req HTTP请求对象
     * @param resp HTTP响应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO异常
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        resp.setContentType("application/json;charset=UTF-8");
        String pathInfo = req.getPathInfo();

        try {
            // 获取科室列表
            if ("/departments".equals(pathInfo)) {
                List<Department> departments = departmentDao.findAll();
                resp.getWriter().write(gson.toJson(departments));
            }
            // 获取医生列表
            else if ("/doctors".equals(pathInfo)) {
                String deptIdStr = req.getParameter("deptId");
                List<DoctorView> doctors;
                
                if (deptIdStr != null && !deptIdStr.trim().isEmpty()) {
                    // 获取指定科室的医生
                    int deptId = Integer.parseInt(deptIdStr);
                    doctors = doctorViewDao.findByDeptId(deptId);
                } else {
                    // 获取所有医生
                    doctors = doctorViewDao.findAll();
                }
                
                resp.getWriter().write(gson.toJson(doctors));
            }
            
        } catch (SQLException e) {
            resp.setStatus(500);
            resp.getWriter().write("{\"error\": \"数据库错误\"}");
            e.printStackTrace();
        } catch (NumberFormatException e) {
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"无效的参数\"}");
        }
    }

    /**
     * 处理更新医生信息的POST请求
     * @param request HTTP请求对象
     * @param response HTTP响应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO异常
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // ... existing code ...
    }
} 